package de.koller.worlders;

import de.koller.oceanFramework.util.MIn;
import de.koller.oceanFramework.util.MOut;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.swing.JOptionPane;

public class User {
	
	private String userpath;
	private String username = null;
	private String mapName = "default";
	
	public User() {
		
		File root = new File( "./user" );
		if( !root.exists() ) {
			JOptionPane.showMessageDialog( null, "Willkommen, dies ist der erste Start des Spieles.\n"
					+ "Spielerdaten werden angelegt.", "Willkommen", JOptionPane.INFORMATION_MESSAGE );
			root.mkdir();
		}
		
		
		while( username == null ) {
			String name = (String)JOptionPane.showInputDialog( null, "Bitte gebe deinen Namen (oder Welt) ein.",
					"Benutzer", JOptionPane.PLAIN_MESSAGE, null,
					null, getDefaultName() );
			if( name == null )
				System.exit( 1 );
			if( name.isEmpty() ) {
				JOptionPane.showMessageDialog( null, "Bitte gebe einen gültigen Namen ein.",
					"Ungültiger Name", JOptionPane.ERROR_MESSAGE );
				continue;
			}
			
			name = name.trim();
			File userData = new File( "./user/" + name );
			if( !userData.exists() ) {
				int i = JOptionPane.showConfirmDialog( null, "Der Benutzer '" + name + "' exitiert noch nicht. "
						+ "Soll er erstellt werden?", "Neuer Benutzer", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE );

				if( i == 2 )
					System.exit( i );
				if( i == 0 ) {
					username = name;
					userData.mkdir();
					
					File[] fs = new File( "./gameData/" ).listFiles();
					if( fs != null )
						for( File f : fs ) {
							try {
								Files.copy( f.toPath(), new File( "./user/" + name + "/" + f.getName() ).toPath() );
							} catch( IOException ex ) {
								ex.printStackTrace();
							}
						}
				}
			} else
				username = name;
		}
		
		saveDefaultName( username );
		userpath = "./user/" + username + "/";
	}

	private String getDefaultName() {
		MIn i = new MIn( "./user/config.dat" );
		String dn = i.readString( 1 )[0];
		i.close();
		
		if( dn == null || dn.isEmpty() )
			return "Default";
		return dn;
	}
	
	private void saveDefaultName( String name ) {
		MOut o = new MOut( "./user/config.dat" );
		o.writeString( name );
		o.close();
	}
	
	public User( String name ) {
		
		File root = new File( "./user" );
		if( !root.exists() ) {
			JOptionPane.showMessageDialog( null, "Willkommen, dies ist der erste Start des Spieles.\n"
					+ "Spielerdaten werden angelegt.", "Willkommen", JOptionPane.INFORMATION_MESSAGE );
			root.mkdir();
		}
		
		
		while( username == null ) {
			File userData = new File( "./user/" + name );
			if( !userData.exists() ) {
				username = name;
				userData.mkdir();
				File[] fs = new File( "./gameData/" ).listFiles();
				for( File f : fs )
					try {
						Files.copy( f.toPath(), userData.toPath() );
					} catch( IOException ex ) {
						ex.printStackTrace();
					}
			} else
				username = name;
		}
		
		userpath = "./user/" + username + "/";
	}
	
	public void endGame( WorldersRoot wroot ) {
		wroot.getWio().save();
	}
	
	public void loadMapName() {
		assert userpath != null;
		
		MIn i = new MIn( userpath + "player.2mp" );
		float x = i.readFloat( 1 )[0];
		float y = i.readFloat( 1 )[0];
		
		int n = i.readInt( 1 )[0];
		if( n == 0 )
			return;
		
		mapName = new String( i.read( n ) );
		i.close();
	}
	
	public String getUsername() {
		return username;
	}

	public String getUserpath() {
		return userpath;
	}

	public String getMapName() {
		return mapName;
	}

	public void setMapName( String mapName ) {
		this.mapName = mapName;
	}
	
}
